package ui.swing;

import java.awt.Color;
import java.awt.Rectangle;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import data.LoadData;
import data.SaleAndBuyPool;
import data.StockPriceSaleAndBuy;
import data.StockPriceSaleAndBuy.DealedPoint;
import data.TimeStock;

public class DealUI extends JFrame implements Runnable{

	private static final long serialVersionUID = -2845637507059379093L;
	private JPanel contentPane;
	private JTable tablePrice;
	private JTable tableDealed;
	private PriceModel priceModel;
	private DealedModel dealedModel;

	private String code;
	private JTextField textField;
	private JTextField textField_1;
	private JScrollPane jsPane2 = new JScrollPane();

	/**
	 * Create the frame.
	 */
	public DealUI(String code) {
		this.code = code;
		TimeStock ts = LoadData.getTimeStock(code);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 454, 572);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("买卖档口");
		label.setBounds(10, 35, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("买卖记录");
		label_1.setBounds(187, 35, 54, 15);
		contentPane.add(label_1);
		
		JScrollPane jsPane = new JScrollPane();
		
		tablePrice = new JTable();
		tablePrice.setShowHorizontalLines(false);
		tablePrice.setShowGrid(false);
		tablePrice.setBackground(Color.WHITE);
		jsPane.setBounds(10, 58, 161, 465);
		jsPane.setViewportView(tablePrice);
		contentPane.add(jsPane);
		
		tableDealed = new JTable();
		tableDealed.setBackground(Color.WHITE);
		jsPane2.setBounds(187, 60, 230, 463);
		jsPane2.setViewportView(tableDealed);
		contentPane.add(jsPane2);
		
		
		priceModel = new PriceModel();
		dealedModel = new DealedModel();
		
		tableDealed.setModel(dealedModel);
		tablePrice.setModel(priceModel);
		
		JLabel label_2 = new JLabel("代码：");
		label_2.setBounds(10, 10, 36, 15);
		contentPane.add(label_2);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(44, 7, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		textField.setText(code);
		
		JLabel label_3 = new JLabel("名称：");
		label_3.setBounds(117, 10, 36, 15);
		contentPane.add(label_3);
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(150, 7, 66, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		textField_1.setText(ts.getStockName());
		
		DefaultTableCellRenderer render = new DefaultTableCellRenderer();
		render.setHorizontalAlignment(SwingConstants.RIGHT);
		tablePrice.getColumn("数量").setCellRenderer(render);
		tableDealed.getColumn("数量").setCellRenderer(render);
		tablePrice.getColumn("价格").setCellRenderer(render);
		tableDealed.getColumn("价格").setCellRenderer(render);
		
		new Thread(this).start();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(3000);
				priceModel.dealDatas();
				dealedModel.dealDatas();
				int rowCount = dealedModel.getRowCount();
				Rectangle rect = tableDealed.getCellRect(rowCount-1, 0, true);
				tableDealed.scrollRectToVisible(rect);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	class PriceModel extends DefaultTableModel{

		private static final long serialVersionUID = -4429480606416804780L;
		private String[] col = {"买卖","价格","数量"};
		private Object[][] datas;
		
		public PriceModel() {
			super();
			StockPriceSaleAndBuy spsa = SaleAndBuyPool.getSab(code);
			Map<BigDecimal, Integer> notDeal = spsa.getListNotDeal();
			datas = new Object[notDeal.size()+1][col.length];
			setDataVector(datas, col);
			dealDatas();
		}
		
		public void dealDatas() {
			TimeStock ts = LoadData.getTimeStock(code);
			StockPriceSaleAndBuy spsa = SaleAndBuyPool.getSab(code);
			Map<BigDecimal, Integer> notDeal = spsa.getListNotDeal();
			Set<BigDecimal> priceSet = notDeal.keySet();
			datas = new Object[notDeal.size()+1][col.length];
			setRowCount(datas.length);
			int i=0;
			for (BigDecimal price : priceSet) {
				if(price.compareTo(ts.getFirstBuyPrice())==0){
					datas[i][0]="当前价";
					datas[i][1] = spsa.getCurPrice().toString();
					datas[i][2] = " ";
					i++;
				}
				if(price.compareTo(ts.getFirstBuyPrice())>0){
					datas[i][0]="卖";
				}else{
					datas[i][0]="买";
				}
				datas[i][1] = price.toString();
				datas[i][2] = notDeal.get(price)/100;
				i++;
			}
			fillValues();
		}
		
		private void fillValues(){
			this.setRowCount(datas.length);
			for (int i = 0; i < datas.length; i++) {
				for (int j = 0; j < datas[i].length; j++) {
					setValueAt(datas[i][j], i, j);
				}
			}
		}
	}
	
	class DealedModel extends DefaultTableModel{

		private static final long serialVersionUID = 7996546141722902307L;
		private String[] col = {"买卖","价格","数量","时间"};
		private Object[][] datas;
		public DealedModel() {
			super();
			StockPriceSaleAndBuy spsa = SaleAndBuyPool.getSab(code);
			Map<BigDecimal, Integer> notDeal = spsa.getListNotDeal();
			datas = new Object[notDeal.size()][col.length];
			setDataVector(datas, col);
			dealDatas();
		}
		
		public void dealDatas() {
			StockPriceSaleAndBuy spsa = SaleAndBuyPool.getSab(code);
			List<DealedPoint> dpList= spsa.getListDealed();
			synchronized (dpList) {
				datas = new Object[dpList.size()][col.length];
				setRowCount(datas.length);
				int i=0;
				for (DealedPoint dealedPoint : dpList) {
					if(dealedPoint.isBuy()){
						datas[i][0] = "买";
					}else{
						datas[i][0] = "卖";
					}
					datas[i][1] = dealedPoint.getPrice().toString();
					datas[i][2] = dealedPoint.getQty()/100;
					datas[i][3] = dealedPoint.getDealTime().toString();
					i++;
				}
			}
			
			fillValues();
		}
		
		private void fillValues(){
			this.setRowCount(datas.length);
			for (int i = 0; i < datas.length; i++) {
				for (int j = 0; j < datas[i].length; j++) {
					setValueAt(datas[i][j], i, j);
				}
			}
		}
	}
}
